﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataAccessLayer;
using StockAdjust;
using System.Transactions;

namespace BusinessLogicLayer
{
    public class IssueStockAdjustmentVoucherControl
    {
        CategoryRepository categoryRepo;
        StationeryRepository stationeryRepo;
        SupplierRepository supplierRepo;
        SupplierDetailRepository supplierDetailRepo;
        UserRepository userRepo;
        EmployeeRepository employeeRepo;
        AdjustmentRepository adjustmentRepo;
        AdjustmentDetailRepository adjustmentDetailRepo;
        StockHistoryRepository stockHistoryRepo;

        public IssueStockAdjustmentVoucherControl()
        {
            categoryRepo = new CategoryRepository();
            stationeryRepo = new StationeryRepository();
            supplierRepo = new SupplierRepository();
            supplierDetailRepo = new SupplierDetailRepository();
            userRepo = new UserRepository();
            employeeRepo = new EmployeeRepository();
            adjustmentRepo = new AdjustmentRepository();
            adjustmentDetailRepo = new AdjustmentDetailRepository();
            stockHistoryRepo = new StockHistoryRepository();
        }

        public List<Category> DisplayAllCategory()
        {
            return categoryRepo.GetAllCategory();
        }

        public List<Stationery> DisplayStationeryByCategoryId(int categoryId)
        {
            return stationeryRepo.GetStationeryByCategoryId(categoryId);
        }

        public List<Supplier> DisplaySupplierByItemCode(string itemCode)
        {
            return supplierRepo.GetSupplierByItemCode(itemCode);
        }

        public User GetUserByUsername(string username)
        {
            return userRepo.GetUserByUsername(username);
        }

        public Employee GetEmployeeByUserId(int userId)
        {
            return employeeRepo.GetEmployeeByUserId(userId);
        }

        public Stationery GetStationeryByItemCode(string itemCode)
        {
            return stationeryRepo.GetStationeryByItemCode(itemCode);
        }

        public void SaveAdjustmentVoucher(User user, List<StockAdjustment> list)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                // Save new Adjustment
                Adjustment adjustment = new Adjustment();
                adjustment.RequestedDate = DateTime.Now;
                adjustment.RequestedBy = employeeRepo.GetEmployeeByUserId(user.UserID).EmployeeID;
                decimal voucherPrice = CalculateVoucherTotal(list);
                if (voucherPrice >= 250)
                {
                    adjustment.Status = "Manager";
                }
                else
                {
                    adjustment.Status = "Supervisor";
                }
                adjustmentRepo.CreateAdjustment(adjustment);

                for (int i = 0; i < list.Count; i++)
                {
                    StockAdjustment adj = list[i];
                    Stationery stationery = stationeryRepo.GetStationeryByItemCode(adj.ItemCode);
                    int currentQty = stationery.AvailableQty;

                    // Save new AdjustmentDetail
                    AdjustmentDetail adjustmentDetail = new AdjustmentDetail();
                    adjustmentDetail.AdjVoucherID = adjustment.AdjVoucherID;
                    adjustmentDetail.ItemCode = adj.ItemCode;
                    int adjustQty = 0;
                    if (adj.AdjustCondition.Equals("Quantity to be adjusted"))
                    {
                        adjustQty = adj.AdjustQuantity;
                    }
                    else
                    {
                        adjustQty = adj.AdjustQuantity - currentQty;
                    }
                    adjustmentDetail.Quantity = adjustQty;
                    adjustmentDetail.Reason = adj.Reason;
                    adjustmentDetail.SupplierID = adj.SupplierId;
                    adjustmentDetailRepo.CreateAdjustmentDetail(adjustmentDetail);
                }
                ts.Complete();
            }
        }

        private decimal CalculateVoucherTotal(List<StockAdjustment> list)
        {
            decimal total = 0;
            foreach (StockAdjustment adj in list)
            {
                SupplierDetail supplierDetail = supplierDetailRepo.GetSupplierDetailBySupplierIdAndItemCode(adj.SupplierId, adj.ItemCode);
                decimal supplierPrice = (decimal)supplierDetail.Price;
                Stationery stationery = stationeryRepo.GetStationeryByItemCode(adj.ItemCode);

                int adjQty = 0;
                if (adj.AdjustCondition.Equals("Quantity to be adjusted"))
                {
                    adjQty = Math.Abs(adj.AdjustQuantity);
                }
                else
                {
                    adjQty = Math.Abs(adj.AdjustQuantity - stationery.AvailableQty);
                }
                total = supplierPrice * adjQty;
            }
            return total;
        }

    }
}
